
const sass = require("sass")
const loadGruntTasks = require("load-grunt-tasks")
const useref = require("useref")
module.exports = (grunt) => {
    const data = {
        fixedPath: "dist",
        menus: [
            {
                name: 'Home',
                icon: 'aperture',
                link: 'index.html'
            },
            {
                name: 'About',
                link: 'about.html'
            },
            {
                name: 'Contact',
                link: '#',
                children: [
                    {
                        name: 'Twitter',
                        link: 'https://twitter.com/w_zce'
                    },
                    {
                        name: 'About',
                        link: 'https://weibo.com/zceme'
                    },
                    {
                        name: 'divider'
                    },
                    {
                        name: 'About',
                        link: 'https://github.com/zce'
                    }
                ]
            }
        ],
        date: new Date(),
        pkg: grunt.file.readJSON('package.json')
    }
    grunt.initConfig({
        clean: {
            dist: ['dist']
        },
        // 编译html文件
        web_swig: {
            options: {
              swigOptions:{
                cache: false
              },
              getData: function(tpl){
                return data
              }
            },
            mian: {
                files: [
                    {
                        expand: true,
                        cwd: "src/",
                        src: "*.html",
                        dest: "dist",
                    }
                ]
            }
        },
        htmlmin: {
            mian: {
                files: [{
                    expand: true,
                    cwd: 'dist/',
                    src: ['*.html'],
                    dest: 'build/',
                    ext: ".html"
                }]
            }
        },
        sass: {
            options: {
                // sourceMap: true,
                implementation: sass
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'src/assets/styles', //需要压缩的css路径
                    src: ['*.scss'], //需要压缩的css
                    dest: 'dist/assets/styles', //压缩之后的路径
                    ext: '.css' //压缩之后的css后缀名
                }]
            }
        },
        cssmin: {
            target: {
                files: [{
                    expand: true,
                    cwd: 'dist/',
                    src: ['**/*.css'],
                    dest: 'dist/',
                    ext: '.min.css'
                }]
            }
        },
        babel: {
            options: {
                // sourceMap: true,
                presets: ["@babel/preset-env"]
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'src/assets/scripts', //需要压缩的js路径
                    src: ['*.js'], //需要压缩的js
                    dest: 'dist/assets/scripts', //压缩之后的路径
                    ext: '.js' //压缩之后的js后缀名
                }]
            }
        },
        uglify: {
            main: {
                files: [
                    {
                        expand: true,     // Enable dynamic expansion.
                        cwd: 'dist/',      // Src matches are relative to this path.
                        src: ['**/*.js'], // Actual pattern(s) to match.
                        dest: 'dist/',   // Destination path prefix.
                        ext: '.min.js',   // Dest filepaths will have this extension.
                        // extDot: 'first'   // Extensions in filenames begin after the first dot
                    },
                ],
            }
        },
        copy: {
            fonts: {
                expand: true,
                src: 'src/assets/fonts',
                dest: data.fixedPath + '/assets/fonts',
            },
            public: {
                expand: true,
                src: 'public',
                dest: data.fixedPath + '/public',
            }
        },
        watch: {
            js: {
                files: ["src/assets/scripts/*.js"],
                //当文件发生变化时去执行的任务 tasks
                tasks: ["babel"]
            },
            css: {
                files: ["src/assets/styles/*.scss"],
                //当文件发生变化时去执行的任务 tasks
                tasks: ["sass"]
            },
            html: {
                files: ["src/*.html"],
                //当文件发生变化时去执行的任务 tasks
                tasks: ["web_swig"]
            }
        },
        imagemin: {
            /* 压缩图片大小 */
            dist: {
                options: {
                    optimizationLevel: 1 //定义 PNG 图片优化水平
                },
                files: [{
                    expand: true,
                    cwd: 'dist/assets/images/',//原图存放的文件夹
                    src: ['*.{png,jpg}'], // 优化 img 目录下所有 png/jpg/jpeg图片
                    dest: 'dist/assets/imagesmin/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                }]
            }
        },
        browserSync: {
            bsFiles: {
                src : [
                    'dist/assets/styles/*.css',
                    'dist/assets/scripts/*.js',
                    'dist/**/*.html'
                ]
            },
            options: {
                port: 9999,
                watchTask: true,
                server: {
                    baseDir: "dist",
                    routes: {
                        "/node_modules": "node_modules"
                    }
                },
            }
        }
    })

    // grunt.loadNpmTasks("grunt-sass")
    // 自动加载所有grunt插件中的任务
    loadGruntTasks(grunt)
    grunt.registerTask("useref", function () {
       
    })
    
    grunt.registerTask("build", ["sass", "babel", "web_swig"])
    grunt.registerTask("serve", ["browserSync", "watch"])
    grunt.registerTask("start", ["build", "browserSync", "watch"])
    grunt.registerTask("deploy", ["build", "cssmin", "uglify"])
    grunt.registerTask("default", ["build"])
}